<template>
  <div ref="songs">
    <div class="songs" v-for="(item, index) in list" :key="index" @click="$router.push(`/songs/${item.id}`)">
      <img class="songimg" v-lazy="item && item.img1v1Url" alt />
      <span>{{item && item.name }}</span>
      <span class="name" v-if="item && item.alias.length">({{item && item.alias[0] }})</span>
      <div class="ren" v-if="item && item.accountId">
        <img src="../../assets/ren.png" alt />
      </div>
    </div>
  </div>
</template>
<script>
export default {
  props: ["keywords", "content"],
  mounted() {
    this.load = this.$loading.show();
    let main = document.getElementsByClassName("searchInfo")[0];
    main.addEventListener("scroll", this.windowScroll, true);
    this.init();
  },
  watch: {
    keywords() {
       this.showlaoding = true;
       this.$api.search({ keywords: this.keywords,type: 100})
        .then((res) => {
          this.list = res.artists
          this.$emit("change", res.artistCount);
          this.load.hide();
        });
    },
  },
  data() {
    return {
      list: [],
      load: "",
      showlaoding: true,
      page: 1,
    };
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.windowScroll, true);
  },
  methods: {
    windowScroll() {
      this.$nextTick(() => {
        let tpScrollTop = document.getElementsByClassName("searchInfo")[0]
          .scrollTop;
        if (!this.showlaoding) {
          if (
            tpScrollTop >
            (this.$refs.songs && this.$refs.songs.offsetHeight - 500)
          ) {
            this.load = this.$loading.show();
            this.page += 1;
            this.init();
          }
        }
      });
    },
    init() {
      this.showlaoding = true;
      this.$api
        .search({
          keywords: this.keywords,
          type: 100,
          offset: (this.page - 1) * 50,
        })
        .then((res) => {
          this.list = this.list.concat(res.artists);
          res.artists && res.artists.length
            ? (this.showlaoding = false)
            : (this.showlaoding = true);
          this.$emit("change", res.artistCount);
          this.load.hide();
        });
    },
  },
};
</script>
<style lang="less" scoped>
.songs {
  height: 60px;
  padding: 10px 30px;
  display: flex;
  align-items: center;
  &:nth-child(2n) {
    background-color: #f5f5f7;
  }
  &:hover {
    background-color: #ebeced;
  }
  .songimg {
    width: 40px;
    height: 40px;
  }
  span {
    margin: 0 5px;
    font-size: 13px;
  }
  .ren {
    border-radius: 50%;
    border: 1px solid #e2bebf;
    display: flex;
    justify-content: center;
    img {
      width: 10px;
      color: red;
      border-radius: 50%;
      margin: 2px;
    }
  }
  .name {
    font-size: 12px;
    color: #9488a1;
  }
}
</style>
